package com.dt76.smallloanbackendservice.service.etao.impl;

import com.dt76.smallloanbackendservice.mapper.etao.EmpRoleMapper;
import com.dt76.smallloanbackendservice.mapper.etao.EmployeeMapper;
import com.dt76.smallloanbackendservice.pojo.etao.CurrentUser;
import com.dt76.smallloanbackendservice.pojo.etao.EmpRole;
import com.dt76.smallloanbackendservice.pojo.etao.Employee;
import com.dt76.smallloanbackendservice.pojo.etao.Role;
import com.dt76.smallloanbackendservice.service.etao.EmployeeService;
import com.dt76.smallloanbackendservice.utils.DateUtils;
import com.dt76.smallloanbackendservice.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmpRoleMapper empRoleMapper;


    @Override
    public Page getPageEmp(String empName, Integer dId, Integer pageIndex) {
        Page page = new Page();
        page.setPageIndex(pageIndex);
        page.setTotalCount(employeeMapper.getEmpCount(dId, empName));
        List<Employee> empPage = employeeMapper.getPageEmp(empName, dId, (page.getPageIndex() - 1) * page.getPageSize(), page.getPageSize());
        for (Employee emp : empPage) {
            Role roles = emp.getRoles();
            emp.setRoleStr(roles.getrName());
            //将DATE型转成字符串型
            Date createTime = emp.getCreateTime();
            emp.setCreateTimeStr(DateUtils.dateToString(createTime, "yyyy-MM-dd"));
        }
        page.setRows(empPage);
        return page;
    }

    @Override
    public boolean updEmpEnable(Integer eId, Integer empEnable) {
        boolean fg = false;
        if (employeeMapper.updateEmpEnable(eId, empEnable) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public Employee getEmpById(Integer eId) {
        return employeeMapper.getEmpById(eId);
    }

    @Override
    public boolean addEmp(Employee employee, Integer currentId) {
        boolean fg = false;
        EmpRole empRole = new EmpRole();
        int isSuccess = employeeMapper.saveEmp(employee);
        //取到刚插入的员工,获取员工id和角色id,再插入到员工角色表中
        Employee newEmp = employeeMapper.getEmpByLoginNmae(employee.getEmpLoginName());
        empRole.seteId(newEmp.geteId());
        empRole.setrId(employee.getRid());
        if (empRoleMapper.saveEmpRole(empRole) == 1 && isSuccess == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public boolean updEmp(Employee employee, Integer currentId) {
        boolean fg = false;
        EmpRole empRole = new EmpRole();
        empRole.seteId(employee.geteId());
        empRole.setrId(employee.getRid());
        if (employeeMapper.updateEmp(employee) == 1 && empRoleMapper.updateEmpRole(empRole) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public boolean delEmp(Integer eId, CurrentUser currentUser) {
        boolean fg = false;
        if (employeeMapper.deleteEmp(eId) == 1 && empRoleMapper.deleteEmpRole(eId) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public Employee getEmpByLoginName(String username) {
        return employeeMapper.getEmpByLoginNmae(username);
    }

    @Override
    public List<Employee> getAllEmp() {
        return employeeMapper.getAllEmp();
    }


}
